package com.amoby.main.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.amoby.base.common.Pager;
import com.amoby.main.entity.BsAccInfo;
import com.amoby.main.entity.Cbaacc;
import com.amoby.main.entity.Cbabill;
import com.amoby.main.entity.SysUsers;
import com.amoby.main.entity.Verifier;
import com.amoby.main.mapper.CbaMapper;
import com.amoby.main.mapper.CbaaccMapper;
import com.amoby.main.mapper.CbabillMapper;
import com.amoby.main.mapper.VerifierMapper;
import com.amoby.main.service.CbaMgrService;

@Service("cbaMgrService")
public class CbaMgrServiceImpl implements CbaMgrService {
    @Autowired
    private CbaMapper cbaMapper;
    @Autowired
    private CbaaccMapper cbaaccMapper;
    @Autowired
    private CbabillMapper cbabillMapper;
    @Autowired
    private VerifierMapper verifierMapper;

    @Override
    public List<BsAccInfo> loadBsAccPage(Pager pager) throws Exception {
        List<BsAccInfo> list = cbaMapper.selectBsAccPage(pager);
        pager.setResult(list);
        return list;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public BsAccInfo saveBsAcc(BsAccInfo entity, SysUsers user) throws Exception {
        entity.setUpuser(user.getUserId());
        entity.setUptime(new Date());
        if (cbaMapper.updateBusiness(entity) != 1) {
            throw new RuntimeException("更新失败");
        }
        return entity;
    }

    @Override
    public List<Cbaacc> loadCbaAccPage(Pager pager) {
        List<Cbaacc> list = cbaaccMapper.selectPage(pager);
        pager.setResult(list);
        return list;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Cbaacc saveCbaAcc(Cbaacc entity, SysUsers user) {
        entity.setUptime(new Date());
        if (entity.getEnable() == 1) {
            // 只允许一个为上线状态
            cbaaccMapper.updateDisableAll(entity);
        }

        if (entity.getId() == null) {
            entity.setCrtime(new Date());
            if (cbaaccMapper.insert(entity) != 1) {
                throw new RuntimeException("保存失败");
            }
        } else {
            entity.setUptime(new Date());
            if (cbaaccMapper.updateByPrimaryKeySelective(entity) != 1) {
                throw new RuntimeException("保存失败");
            }
        }

        return entity;
    }

    @Override
    public void loadCbabillPage(Pager pager) {
        pager.setResult(cbabillMapper.selectPageList(pager.getParamsMap()));
        pager.setTotalRecord(cbabillMapper.selectPageCount(pager.getParamsMap()));
    }

    @Override
    public Cbabill saveCbabill(Cbabill entity, SysUsers user) {
        if (entity.getId() == null) {
            throw new IllegalArgumentException("id must be not null");
        }

        entity.setUptime(new Date());
        if (cbabillMapper.updateCbabill(entity) != 1) {
            throw new RuntimeException("保存失败");
        }
        return entity;
    }

    // 联邦银行审核员查询列表
    @Override
    public void getAllVerifier(Pager pager) {
        pager.setResult(verifierMapper.getAllVerifier(pager));
    }

    @Override
    public int delVerifier(Long id) {
        return verifierMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int addVerifier(Verifier verifier) {
        return verifierMapper.insert(verifier);
    }

    @Override
    public int updVerifier(Verifier verifier) {
        return verifierMapper.updateByPrimaryKeySelective(verifier);
    }

    @Override
    public Integer countStatus(Map<String, Object> map) {
        return verifierMapper.countStatus(map);
    }

    @Override
    public SysUsers getVerifier() {
        return null;
    }

    @Override
    public Integer checkName(Map<String, Object> map) {
        return verifierMapper.checkName(map);
    }

    @Override
    public void update() {
        verifierMapper.update();
    }

    @Override
    public List<Map<String, Object>> exportAll(Map<String, Object> map) {
        return cbabillMapper.exportAll(map);
    }
}
